package cn.edu.sgu.www.mhxysy.service.repository.impl;

import cn.edu.sgu.www.mhxysy.entity.repository.PackageGrid;
import cn.edu.sgu.www.mhxysy.entity.repository.RolePackage;
import cn.edu.sgu.www.mhxysy.mapper.repository.PackageGridMapper;
import cn.edu.sgu.www.mhxysy.mapper.repository.RolePackageMapper;
import cn.edu.sgu.www.mhxysy.service.repository.PackageGridService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class PackageGridServiceImpl implements PackageGridService {

    private final PackageGridMapper packageGridMapper;
    private final RolePackageMapper rolePackageMapper;

    @Autowired
    public PackageGridServiceImpl(
            PackageGridMapper packageGridMapper,
            RolePackageMapper rolePackageMapper) {
        this.packageGridMapper = packageGridMapper;
        this.rolePackageMapper = rolePackageMapper;
    }

    @Override
    public List<PackageGrid> selectByRoleId(String roleId) {
        // 查询角色包裹
        QueryWrapper<RolePackage> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("role_id", roleId);

        RolePackage rolePackage = rolePackageMapper.selectOne(queryWrapper);

        if (rolePackage != null) {
            // 通过包裹ID查询仓库格子
            QueryWrapper<PackageGrid> gridQueryWrapper = new QueryWrapper<>();

            gridQueryWrapper.orderByAsc("location");
            gridQueryWrapper.eq("package_id", rolePackage.getId());

            return packageGridMapper.selectList(gridQueryWrapper);
        }

        return null;
    }

}